<?php
/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 * 
 * =================================================================
 *                 MAGENTO EDITION USAGE NOTICE
 * =================================================================
 * This package designed for Magento COMMUNITY edition
 * aheadWorks does not guarantee correct work of this extension
 * on any other Magento edition except Magento COMMUNITY edition.
 * aheadWorks does not provide extension support in case of
 * incorrect edition usage.
 * =================================================================
 *
 * @category   AW
 * @package    AW_AdvancedReviews
 * @copyright  Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
 */

require_once 'Mage/Review/controllers/ProductController.php';

class AW_AdvancedReviews_ProductController extends Mage_Review_ProductController
{
    public function postAction()
    {
        if(Mage::helper('advancedreviews')->getAntiSpamEnabled())
            $bot = ($this->getRequest()->getPost('_antispam'))? false : true;
        else
            $bot = false;

        if(!$bot){
            if ( $data = $this->getRequest()->getPost() )
            {
                //Save PROSCONS data in customer session
                if ( isset( $data['proscons_items'] ) )
                {
                    Mage::register( 'advancedreviews_proscons_new_items', $data['proscons_items'] );
                }
                if ( isset( $data['user-pros'] ) )
                {
                    Mage::register('advancedreviews_proscons_user_pros', $data['user-pros'] );
                }
                if ( isset( $data['user-cons'] ) )
                {
                    Mage::register('advancedreviews_proscons_user_cons', $data['user-cons'] );
                }
                if ( isset( $data['recommend'] ) )
                {
                    Mage::register('advancedreviews_recommend_value', $data['recommend'] );
                }
                if(isset($data['email']))
                    Mage::register('advancedreviews_guest_email', $data['email'] );
            }

            //Here allow to insert review
            Mage::getSingleton('review/session')->setRedirectUrl(Mage::helper('advancedreviews')->getReviewsBackUrl());
            parent::postAction();
        /*
         * Logical functional finished in AW_AdvancedReviews_Model_Mysql4_Review
         * throw overrided _afterSave() of Mage_Review_Model_Mysql4_Review
         */
        }
    }

    public function viewAction(){
        $review = $this->_loadReview((int) $this->getRequest()->getParam('id'));
        if (!$review) {
            $this->_forward('noroute');
            return;
        }

        $product = $this->_loadProduct($review->getEntityPkValue());
        if (!$product) {
            $this->_forward('noroute');
            return;
        }

        $this->loadLayout();
        $this->_initLayoutMessages('review/session');
        $this->_initLayoutMessages('catalog/session');
        $this->getLayout()->getBlock('head')->setTitle($product->getMetaTitle() .' - '. $review->getTitle() . $this->__(' - review by ') . $review->getNickname());
        $this->renderLayout();
    }
    protected function _loadReview($reviewId)
    {
        if (!$reviewId) {
            return false;
        }

        $review = Mage::getModel('review/review')->load($reviewId);
        /* @var $review Mage_Review_Model_Review */
        if (!$review->getId() || !$review->getStatusId() == Mage_Review_Model_Review::STATUS_APPROVED || !in_array(Mage::app()->getStore()->getId(), (array)$review->getStores())) {
            return false;
        }

        Mage::register('current_review', $review);

        return $review;
    }

    protected function _loadProduct($productId)
    {
        if (!$productId) {
            return false;
        }

        $product = Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($productId);
        /* @var $product Mage_Catalog_Model_Product */
        if (!$product->getId() || !$product->isVisibleInCatalog() || !$product->isVisibleInSiteVisibility()) {
            return false;
        }

        Mage::register('current_product', $product);
        Mage::register('product', $product);

        return $product;
    }


}
